from flask import Flask

def create_app(config_name=None):
    """Flask应用工厂函数"""
    
    app = Flask(__name__)
    
    # 加载配置
    if config_name is None:
        import os
        config_name = os.environ.get('FLASK_ENV') or 'default'
    
    from config import config
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    
    # 注册蓝图
    from app.routes import main_bp, api_bp
    app.register_blueprint(main_bp)
    app.register_blueprint(api_bp, url_prefix='/api')
    
    # 初始化服务
    from app.services import directory_scanner, image_processor, cache_manager
    directory_scanner.init_app(app)
    image_processor.init_app(app)
    cache_manager.init_app(app)
    
    return app